/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.team1160.breakaway.camera;

import com.team1160.breakaway.api.Constants;
import edu.wpi.first.wpilibj.image.EllipseMatch;
import java.util.Vector;

/**
 * This class checks if the area ratio between the two
 * largests ellipses match some pre-computed idea of a ratio
 * constant.
 *
 * NOTE: RATIO COULD BE WRONG!! Requires testing with images.
 * 
 * @author nttoole
 */
public class EllipseAreaPartitioner
{
    public static final double MAGIC_RATIO = Targetting.AREA_RATIO;

    public static final double DELTA = Constants.ERROR_DELTA;

    public EllipseAreaPartitioner()
    {
    }


    public boolean getEllipsesWithSameAreaRatio(Vector ellipses)
    {
        boolean allIsGood = false;
        
        EllipseMatch ellipseLarge, ellipseSmall;

        if (ellipses.size() > 1)
        {
            ellipseLarge = (EllipseMatch) ellipses.elementAt(0);
            ellipseSmall = (EllipseMatch) ellipses.elementAt(1);


           double areaLarge = ellipseLarge.m_majorRadius *
                              ellipseLarge.m_minorRadius;
           double areaSmall = ellipseSmall.m_majorRadius *
                              ellipseSmall.m_minorRadius;

           allIsGood = satisfiesRatio(areaLarge, areaSmall);
        }


        return allIsGood;
    }

    protected boolean satisfiesRatio(double area1, double area2)
    {
        boolean success = false;

        double ratioA = area1 / area2;
        double ratioB = area2 / area1;

        /*if (Math.abs(ratioA - MAGIC_RATIO) < DELTA)
            success = true;
        else if (Math.abs(ratioB - MAGIC_RATIO) < DELTA)
            success = true;*/
        if(ratioB > MAGIC_RATIO)
            success = true;
        else if(ratioA > MAGIC_RATIO)
            success = true;

        return success;

    }
}
